package co.yixiang.yshop.module.tables.controller.admin.reservation;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.common.pojo.CommonResult;
import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;

import co.yixiang.yshop.framework.excel.core.util.ExcelUtils;

import co.yixiang.yshop.framework.operatelog.core.annotations.OperateLog;
import static co.yixiang.yshop.framework.operatelog.core.enums.OperateTypeEnum.*;

import co.yixiang.yshop.module.tables.controller.admin.reservation.vo.*;
import co.yixiang.yshop.module.tables.dal.dataobject.reservation.ReservationDO;
import co.yixiang.yshop.module.tables.convert.reservation.ReservationConvert;
import co.yixiang.yshop.module.tables.service.reservation.ReservationService;

@Tag(name = "管理后台 - 预约记录")
@RestController
@RequestMapping("/tables/reservation")
@Validated
public class ReservationController {

    @Resource
    private ReservationService reservationService;

    @PostMapping("/create")
    @Operation(summary = "创建预约记录")
    @PreAuthorize("@ss.hasPermission('tables:reservation:create')")
    public CommonResult<Integer> createReservation(@Valid @RequestBody ReservationCreateReqVO createReqVO) {
        return success(reservationService.createReservation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新预约记录")
    @PreAuthorize("@ss.hasPermission('tables:reservation:update')")
    public CommonResult<Boolean> updateReservation(@Valid @RequestBody ReservationUpdateReqVO updateReqVO) {
        reservationService.updateReservation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除预约记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('tables:reservation:delete')")
    public CommonResult<Boolean> deleteReservation(@RequestParam("id") Integer id) {
        reservationService.deleteReservation(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得预约记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('tables:reservation:query')")
    public CommonResult<ReservationRespVO> getReservation(@RequestParam("id") Integer id) {
        ReservationDO reservation = reservationService.getReservation(id);
        return success(ReservationConvert.INSTANCE.convert(reservation));
    }

    @GetMapping("/list")
    @Operation(summary = "获得预约记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('tables:reservation:query')")
    public CommonResult<List<ReservationRespVO>> getReservationList(@RequestParam("ids") Collection<Integer> ids) {
        List<ReservationDO> list = reservationService.getReservationList(ids);
        return success(ReservationConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得预约记录分页")
    @PreAuthorize("@ss.hasPermission('tables:reservation:query')")
    public CommonResult<PageResult<ReservationRespVO>> getReservationPage(@Valid ReservationPageReqVO pageVO) {
        PageResult<ReservationDO> pageResult = reservationService.getReservationPage(pageVO);
        return success(ReservationConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出预约记录 Excel")
    @PreAuthorize("@ss.hasPermission('tables:reservation:export')")
    @OperateLog(type = EXPORT)
    public void exportReservationExcel(@Valid ReservationExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ReservationDO> list = reservationService.getReservationList(exportReqVO);
        // 导出 Excel
        List<ReservationExcelVO> datas = ReservationConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "预约记录.xls", "数据", ReservationExcelVO.class, datas);
    }

}
